<template>
    <div>
        <Card :bordered="false">
            <div class="proname">
                <Row>
                    <i-col class="proname-title" :lg="{ span: 24}">
                        <h3>{{ ProName }}</h3>
                    </i-col>
                </Row>
            </div>
        </Card>
        <global-tables
            @changePage="handleChangePage"
            :total="total"
            @handleAllSelect="handleAllSelect"
            :height="tableHeight"
            :list="list"
            :loading="loading"
            :columns="columns"
            :current="search.Page"
        >
            <Form :model="search" :label-width="80">
                <div class="table-top-wrap">
                    <div>
                        <div>
                            <Button class="features-btn" @click="handleBtnClick(null,'add')">
                                <Icon type="md-add" size="18" />新建
                            </Button>
                            <Button class="features-btn btn-auto-m" @click="handleBtnClick(null,'delete')">
                                <Icon class="btn btn-auto-m" type="md-trash" size="18" />删除
                            </Button>
                            <Button class="features-btn btn-auto-m" @click="$router.go(-1)">
                                <Icon class="btn btn-auto-m" type="ios-arrow-back" size="18" />返回
                            </Button>
                        </div>
                    </div>
                    <div class="table-top-wrap-bottom"></div>
                </div>
            </Form>
        </global-tables>
        <Modal :mask-closable="false" v-model="isModal" :title="ModalTitle" width="700">
            <div v-if="isModal">
                <Form ref="formItem" :model="formItem" :rules="ruleValidate" :label-width="100">
                    <Row>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="规格名称" prop="SpecName">
                                <i-input v-model="formItem.SpecName" placeholder="请输入规格名称"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="原价" prop="PriceOld">
                                <InputNumber class="input-number" v-model="formItem.PriceOld" placeholder="请输入原价"></InputNumber>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="厂家货号" prop="SupCode">
                                <i-input v-model="formItem.SupCode" placeholder="请输入厂家货号"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="现价" prop="PriceNew">
                                <InputNumber class="input-number" v-model="formItem.PriceNew" placeholder="请输入现价"></InputNumber>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="进货单位" prop="UnitBuy">
                                <i-input v-model="formItem.UnitBuy" placeholder="请输入进货单位"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="拆分价" prop="PriceUnit">
                                <InputNumber class="input-number" v-model="formItem.PriceUnit" placeholder="请输入拆分价"></InputNumber>
                            </FormItem>
                        </i-col>
                    </Row>
                    <Row>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="销售单位" prop="UnitSales">
                                <i-input v-model="formItem.UnitSales" placeholder="支、个、盒、箱、次，片"></i-input>
                            </FormItem>
                        </i-col>

                        <i-col :lg="{ span: 12 }">
                            <FormItem label="佣金" prop="Commission">
                                <InputNumber class="input-number" v-model="formItem.Commission" placeholder="请输入佣金"></InputNumber>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="换算系数" prop="UnitCount">
                                <InputNumber style="width:100%" class="input-number" v-model="formItem.UnitCount" placeholder="请输入换算系数"></InputNumber>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="警戒库存" prop="StockWarn">
                                <InputNumber class="input-number" v-model="formItem.StockWarn" placeholder="请输入警戒库存"></InputNumber>
                            </FormItem>
                        </i-col>
                    </Row>
                </Form>
            </div>
            <div slot="footer">
                <Button size="large" @click="handleClose">取消</Button>
                <Button type="primary" size="large" @click="handleSubmit('formItem')">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
/**
 * 商品规格
 */
import GlobalTables from '@/components/GlobalTables';
import Api from './service';
export default {
    name: 'ProductListNorm',
    components: {
        GlobalTables
    },
    data() {
        return {
            list: [],
            tableHeight: window.innerHeight - 300,
            itemId: 0,
            allSelectArr: null,
            ProName: sessionStorage.getItem('ProName'),
            loading: true,
            formItem: {},
            ruleValidate: {
                SpecName: [
                    {
                        required: true,
                        message: '请输入内容',
                        trigger: 'change'
                    }
                ],
                StockWarn: [
                    {
                        required: true,
                        message: '请输入内容',
                        type: 'number',
                        trigger: 'change'
                    }
                ],
                SupCode: [
                    {
                        required: true,
                        message: '请输入内容',
                        trigger: 'change'
                    }
                ],
                UnitSales: [
                    {
                        required: true,
                        message: '请输入内容',
                        trigger: 'change'
                    }
                ],
                UnitBuy: [
                    {
                        required: true,
                        message: '请输入内容',
                        trigger: 'change'
                    }
                ],
                UnitCount: [
                    {
                        required: true,
                        message: '请输入内容',
                        type: 'number',
                        trigger: 'change'
                    }
                ],
                PriceUnit: [
                    {
                        required: true,
                        message: '请输入内容',
                        type: 'number',
                        trigger: 'change'
                    }
                ],
                Commission: [
                    {
                        required: true,
                        type: 'number',
                        message: '请输入内容',
                        trigger: 'change'
                    }
                ],
                PriceOld: [
                    {
                        required: true,
                        type: 'number',
                        message: '请输入原价',
                        trigger: 'change'
                    }
                ],
                PriceNew: [
                    {
                        required: true,
                        type: 'number',
                        message: '请输入现价',
                        trigger: 'change'
                    }
                ]
            },
            isEdit: false,
            isProhibit: false,
            total: 0,
            isModal: false,
            ModalTitle: '新建会员',
            search: {
                pid: '',
                pageindex: 1,
                pagesize: 10
            },
            /* 表头 */
            columns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '规格名称',
                    key: 'SpecName'
                },
                {
                    title: '厂家货号',
                    key: 'SupCode'
                },
                {
                    title: '销售单位',
                    width: 200,
                    key: 'UnitSales',
                    render: (h, params) => {
                        const { UnitSales, UnitBuy, UnitCount } = params.row;
                        if (UnitSales == UnitBuy) {
                            return h('div', [h('p', {}, UnitSales)]);
                        } else {
                            return h('div', [h('p', {}, `1${UnitBuy} =  ${UnitCount} ${UnitSales} `)]);
                        }
                    }
                },
                {
                    title: '销售价',
                    key: 'PriceNew'
                },
                {
                    title: '佣金',
                    key: 'Commission'
                },
                {
                    title: '警戒库存',
                    key: 'StockWarn'
                },
                {
                    title: '操作',
                    width: 100,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h(
                                'Button',
                                {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        background: '#5ab1ef',
                                        border: 'none',
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.handleBtnClick(params.row, 'edit');
                                        }
                                    }
                                },
                                '修改'
                            )
                        ]);
                    }
                }
            ]
        };
    },
    methods: {
        /* 关闭对话框 */
        handleClose() {
            this.isModal = false;
        },
        /* 提交 */
        handleSubmit(name) {
            const { formItem, itemId } = this;
            this.$refs[name].validate(valid => {
                if (valid) {
                    let data = {
                        ...formItem,
                        ID: itemId,
                        PID: parseInt(this.$route.params.id),
                        ProName: this.$route.params.name,
                        ProCode: this.$route.params.code
                    };
                    Api.addNormTablesItem(data).then(res => {
                        if (res.state == 'success') {
                            this.$Message.success('操作成功');
                            this.loading = true;
                            this.isModal = false;
                            this.getTablesListData();
                        } else {
                            this.$Message.error(res.message);
                        }
                    });
                }
            });
        },
        /* 表格数据请求 */
        getTablesListData() {
            const data = this.search;
            Api.getNormTablesList(data).then(res => {
                this.list = res.data.rows;
                this.loading = false;
                this.total = res.data.records;
            });
        },
        /* 表格查询 */
        handleSearch() {
            this.loading = true;
            this.getTablesListData();
        },
        /* 表格分页方法 */
        handleChangePage(type, page) {
            this.loading = true;
            if (type == 'page') {
                this.search.pageindex = page;
                this.getTablesListData();
            } else {
                this.search.pagesize = page;
                this.getTablesListData();
            }
        },
        /* 对话框 */
        handleBtnClick(item, type) {
            this.ModalTitle = '新建规格';
            this.formItem = this.initFormItem();
            if (type == 'edit' || type == 'add') {
                this.itemId = 0;
                this.isModal = true;
                if (type == 'edit') {
                    this.itemId = item.ID;
                    this.ModalTitle = '编辑规格';
                    this.formItem = item;
                }
            } else if (type === 'delete') {
                if (this.allSelectArr) {
                    this.$Modal.confirm({
                        title: `你确定要删除吗`,
                        onOk: () => {
                            Api.deleteNormItem({
                                ids: this.allSelectArr
                            }).then(res => {
                                if (res.state == 'success') {
                                    this.$Message.success(res.message);
                                    this.allSelectArr = '';
                                } else {
                                    this.$Message.error(res.message);
                                }
                                this.getTablesListData();
                            });
                        },
                        onCancel: () => {
                            this.$Message.info('取消成功');
                        }
                    });
                } else {
                    this.$Message.error('请先选择需要操作的记录');
                }
            }
        },
        /* 表单数据 */
        initFormItem() {
            return {
                SpecName: '',
                PriceOld: 0,
                UnitCount: 1,
                PriceNew: 0,
                Commission: 0,
                PriceUnit: 0,
                StockWarn: 0
            };
        },
        /* 表单校验 */
        initRuleValidate() {
            return {};
        },
        /* 复选框选中 */
        handleAllSelect(list) {
            let arr = [];
            for (let item in list) {
                arr.push(list[item].ID);
                arr.join(',');
            }
            arr = arr.join(',');
            this.allSelectArr = arr;
        }
    },
    mounted() {
        const { id } = this.$route.params;
        this.search.pid = id;
        /* 列表请求 */
        this.getTablesListData();
    }
};
</script>

<style lang="less" scoped>
.input-number {
    width: 185px;
}

.proname-title {
    width: 100%;
}
</style>

